package com.letv.core.network.volley;

import com.letv.core.bean.LetvBaseBean;
import com.letv.core.network.volley.VolleyResponse.CacheResponseState;
import com.letv.core.network.volley.VolleyResponse.NetworkResponseState;
import com.letv.core.network.volley.VolleyResponse.ResponseSupplier;
import com.letv.core.network.volley.listener.ResponseDelivery;
import com.letv.core.utils.LogInfo;

public class BaseRequestData<T extends LetvBaseBean> {
    /**
     * 请求提供者
     */
    private final ResponseSupplier mSupplier;

    /**
     * 响应分发
     */
    private final ResponseDelivery mDelivery;

    private final String log;

    public BaseRequestData(ResponseSupplier supplier, ResponseDelivery delivery) {
        mSupplier = supplier;
        mDelivery = delivery;

        if (mSupplier == null) {
            throw new NullPointerException("ResponseSupplier in BaseDispatcher is Null");
        }

        if (mDelivery == null) {
            throw new NullPointerException("ResponseDelivery in BaseDispatcher is Null");
        }

        log = mSupplier == ResponseSupplier.NETWORK ? "network" : "cache";
    }

    /**
     * 是否验证成功
     * 
     * @param request
     * @return
     */
    boolean isValidateSuccess(VolleyRequest<?> request) {
        if (request == null) {
            return false;
        }

        if (!request.isValidateSuccess()) {
            postError(request, NetworkResponseState.PRE_FAIL);
            request.finish();
            return false;
        }

        return true;
    }

    /**
     * 响应是否有效
     * 
     * @param request
     * @param response
     * @return
     * @throws InterruptedException
     */
    boolean isResponseAvailabe(VolleyRequest<?> request, VolleyResponse response) {
        return (request != null && response != null && response.success);
    }

    /**
     * 是否已经被取消
     * 
     * @return
     */
    boolean isCanceled(VolleyRequest<?> request) {
        if (request == null) {
            return true;
        }

        if (request.isCanceled()) {
            request.finish();
            return true;
        }

        return false;
    }

    /**
     * 请求成功回调
     * 
     * @param request
     * @param entry
     */
    void postResponse(VolleyRequest<?> request) {
        if (request == null) {
            return;
        }

        mDelivery.postResponse(request, mSupplier);
        LogInfo.log("from " + log + " success:" + request.getUrl());
    }

    /**
     * 请求错误回调
     * 
     * @param request
     */
    void postError(VolleyRequest<?> request, NetworkResponseState responseState) {
        if (request == null) {
            return;
        }

        mDelivery.postError(request, responseState);
        LogInfo.log("from " + log + " error:" + request.getUrl());
    }

    /**
     * 请求错误回调
     * 
     * @param request
     */
    void postError(VolleyRequest<?> request, CacheResponseState responseState) {
        if (request == null) {
            return;
        }

        mDelivery.postError(request, responseState);
        LogInfo.log("from " + log + " error:" + request.getUrl());
    }
}
